package com.alangyun.remind.service.impl;

import java.io.File;

import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.alangyun.remind.BaseActor;
import com.alangyun.remind.error.MindException;
import com.alangyun.remind.repository.StoreEngine;
import com.alangyun.remind.repository.cache.EmployeeCache;
import com.alangyun.remind.repository.cache.LeaderCache;
import com.alangyun.remind.repository.cache.OrganCache;
import com.alangyun.remind.repository.cache.TalentCache;
import com.alangyun.remind.service.IUploadService;
import com.alangyun.remind.service.excel.EmployeeCautionReader;
import com.alangyun.remind.service.excel.ExcelReader;
import com.alangyun.remind.service.excel.LeaderCautionReader;
import com.alangyun.remind.service.excel.OrganCautionReader;
import com.alangyun.remind.service.excel.ParseResult;
import com.alangyun.remind.service.excel.TalentCautionReader;

@Service
public class UploadService extends BaseActor implements IUploadService {

	private ExcelReader organParser = new OrganCautionReader();
	private ExcelReader talentParser = new TalentCautionReader();
	private ExcelReader employeeParser = new EmployeeCautionReader();
	private ExcelReader leaderParser = new LeaderCautionReader();

	@Override
	public void parseOrganFile(MultipartFile file) {
		try {
			File excelFile = StoreEngine.saveFile(file);
			ParseResult result = organParser.read(excelFile);
			if (result.hasError())
				throw new MindException(result.toString());

			StoreEngine.saveDataset(result.getObject(), file.getOriginalFilename());
			OrganCache.my().clear();
		} catch (MindException ex) {
			throw ex;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public void parseTalentFile(MultipartFile file) {
		try {
			File excelFile = StoreEngine.saveFile(file);
			ParseResult result = talentParser.read(excelFile);
			if (result.hasError())
				throw new MindException(result.toString());

			StoreEngine.saveDataset(result.getObject(), file.getOriginalFilename());
			TalentCache.my().clear();
		} catch (MindException ex) {
			throw ex;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public void parseLeaderFile(MultipartFile file) {
		try {
			File excelFile = StoreEngine.saveFile(file);
			ParseResult result = leaderParser.read(excelFile);
			if (result.hasError())
				throw new MindException(result.toString());

			StoreEngine.saveDataset(result.getObject(), file.getOriginalFilename());
			LeaderCache.my().clear();
		} catch (MindException ex) {
			throw ex;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public void parseEmployeeFile(MultipartFile file) {
		try {
			File excelFile = StoreEngine.saveFile(file);
			ParseResult result = employeeParser.read(excelFile);
			if (result.hasError())
				throw new MindException(result.toString());

			StoreEngine.saveDataset(result.getObject(), file.getOriginalFilename());
			EmployeeCache.my().clear();
		} catch (MindException ex) {
			throw ex;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

}
